package cn.edu.cdp.gui.combo_box;

import cn.edu.cdp.dao.MajorDao;
import cn.edu.cdp.domain.Major;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.util.List;
import java.util.Map;

@Component
public class ComboBoxMajor extends JComboBox<String> {
    @Autowired
    public ComboBoxMajor(MajorDao majorDao) {
        super();
        refreshData(majorDao);
    }

    public void refreshData(MajorDao majorDao) {
        this.removeAllItems();

        this.addItem("");
        List<Map<String, Object>> maps = majorDao.allMajor();
        for (Map<String, Object> map : maps) {
            this.addItem((String) map.get("name"));
        }
    }

    public void filter(List<Major> majorList) {
        this.removeAllItems();

        this.addItem("");
        for (Major major : majorList) {
            this.addItem(major.getName());
        }
    }
}
